package com.updateUser.service.impl;

import com.updateUser.common.SequenceUtils;
import com.updateUser.payload.RestResult;
import com.updateUser.payload.UpdateUser;
import com.updateUser.entity.User;
import com.updateUser.mapper.UserMapper;
import com.updateUser.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-09-06
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public List<User> getAllUser() {
        return userMapper.getAllUser();
    }

    @Override
    public RestResult updateUserById(UpdateUser updateUser) {
        Integer i = userMapper.updateUserById(updateUser.getId(), updateUser.getUsername());
        return i > 0 ? new RestResult(200,"修改成功") : new RestResult(400,"修改失败");
    }

    @Override
    public RestResult addUser(User user) {
        SequenceUtils sequenceUtils = new SequenceUtils();
        Integer i = userMapper.addUser(sequenceUtils.nextId(),user.getUsername(),user.getPassword(),user.getHeadImgUrl(),user.getInvitationCode(),user.getGender(),
                user.getAddress(),user.getEnabled(),user.getIsAdmin(),user.getIsPartner(),user.getSiteId(),0);
        return i > 0 ? new RestResult(200,"新增成功") : new RestResult(400,"新增失败");
    }

    @Override
    public RestResult delUser(Long id) {
        Integer i = userMapper.delUser(id);
        return i > 0 ? new RestResult(200,"删除成功") : new RestResult(400,"删除失败");
    }
}
